/*
	File:		PauseState.cpp
	Course:		SGD 1301
	Author:		Peter Nguyen
	Purpose:	CPauseState class handles the pause screen
*/

#include "PauseState.h"
#include "MainMenuState.h"
#include "HowToPlayState.h"
#include "OptionState.h"

#include "../SGD Wrappers/CSGD_DirectInput.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"

#include "Game.h"
#include "GameplayState.h"
#include "BitmapFont.h"

#include <cassert>


////////////////////////////////////////////////////////////
// SINGLETON!
/*static*/ CPauseState* CPauseState::GetInstance( void )
{
	// Static local object
	static CPauseState s_Instance;

	return &s_Instance;
}


////////////////////////////////////////////////////////////
// CONSTRUCTOR
CPauseState::CPauseState(void)
{
	m_pDI		= nullptr;
	m_pTM		= nullptr;
	m_pFont		= nullptr;
	
	
	m_nCursorImageID	= -1;
	
	m_nCursorPos		= 0;
}


////////////////////////////////////////////////////////////
// DESTRUCTOR
CPauseState::~CPauseState(void)
{
}


////////////////////////////////////////////////////////////
// Enter				- load resources
/*virtual*/ void CPauseState::Enter( void )
{
	// Store wrappers
	m_pDI		= CSGD_DirectInput::GetInstance();
	m_pTM		= CSGD_TextureManager::GetInstance();
	m_pFont		= CGame::GetInstance()->GetFont();
	
	// Load assets
	m_nCursorImageID	= m_pTM->LoadTexture( 
							_T("resource/graphics/PN_MenuCursor.png") );
	
	// Set other members
	m_nCursorPos		= 0;
	m_nMenuSize			= 4;
}

////////////////////////////////////////////////////////////
// Exit					- unload resources
/*virtual*/ void CPauseState::Exit( void )
{
	// Unload the assets
	if( m_nCursorImageID != -1 )
	{
		m_pTM->UnloadTexture( m_nCursorImageID );
		m_nCursorImageID = -1;
	}


	// Reset the wrappers to null
	m_pDI		= nullptr;
	m_pTM		= nullptr;
	m_pFont		= nullptr;
}

////////////////////////////////////////////////////////////
// Input			- handle user input
/*virtual*/ bool CPauseState::Input( void )
{
	if( m_pDI->KeyPressed( DIK_UP ) )
	{
		--m_nCursorPos;
		if(m_nCursorPos<0)
			m_nCursorPos = m_nMenuSize - 1;
	}
	else if( m_pDI->KeyPressed( DIK_DOWN ) )
	{
		++m_nCursorPos;
		if(m_nCursorPos == m_nMenuSize)
			m_nCursorPos = 0;
	}

	if( m_pDI->KeyPressed( DIK_ESCAPE ) || m_pDI->KeyPressed( DIK_P ) )
	{
		CGame::GetInstance()->PopState();
		return true;
	}

	if( m_pDI->KeyPressed( DIK_RETURN ) )
	{
		switch( m_nCursorPos )
		{
		case 0:		// Resume
			{
				CGame::GetInstance()->PopState();
				return true;
			}
			break;
			
		case 1:		// How To Play
			{
				CGame::GetInstance()->PushState( CHowToPlayState::GetInstance() );
				return true;
			}
			break;
		case 2:		// Option
			{
				CGame::GetInstance()->PushState( COptionState::GetInstance() );
				return true;
			}
			break;
		case 3:		// MainMenu
			{
				CGame::GetInstance()->PopState();//back to game
				CGame::GetInstance()->PopState();//back to selection
				CGame::GetInstance()->PopState();//back to main menu
				return true;
			}
			break;
		}
	}


	return true;
}				

////////////////////////////////////////////////////////////
// Update			- update game entities
/*virtual*/ void CPauseState::Update( float fElapsedTime )
{
}

////////////////////////////////////////////////////////////
// Render			- render game entities
/*virtual*/ void CPauseState::Render( void )
{
	CGame::GetInstance()->TopState()->Render();

	// Render the menu
	m_pFont->Draw( _T("Pause Screen"), 100, 100, 1.0f, D3DCOLOR_ARGB( 255, 255, 255, 255 ) );

	int space = 50;
	int start = 200;
	int current = start;

	m_pFont->Draw( _T("Resume"),		100, current+=space, 1.0f, D3DCOLOR_ARGB( 255, 255, 255, 255 ) );
	m_pFont->Draw( _T("How To Play"),	100, current+=space, 1.0f, D3DCOLOR_ARGB( 255, 255, 255, 255 ) );
	m_pFont->Draw( _T("Option"),		100, current+=space, 1.0f, D3DCOLOR_ARGB( 255, 255, 255, 255 ) );
	m_pFont->Draw( _T("MainMenu"),		100, current+=space, 1.0f, D3DCOLOR_ARGB( 255, 255, 255, 255 ) );

	// Render the cursor
	int nCursorY = (int)(start + space*(m_nCursorPos)+space*1.2f);
	m_pTM->Draw( m_nCursorImageID, space, nCursorY );
}
	